#include "ScnsIic.c.h"

#if defined(SCNS_IIC_ENABLE)&&SCNS_IIC_ENABLE==1

#include "stm32f7xx_hal.h"
#include "ScnsMath.h"

const ScnsBspIicConfig scnsBspIicConfig[SCNS_IIC_MAX]={
    [SCNS_IIC_SOFT_CT]={.tick=200},
    [SCNS_IIC_SOFT_1]={.tick=10},
    [SCNS_IIC_SOFT_DCMI]={.tick=50},
};

void scnsBspIicSetSdaDir(ScnsIicEnum iicN,uint8 in)
{
    switch(iicN)
    {
        case SCNS_IIC_SOFT_CT:
        {
            const uint32 hb=scnsMathGetHighest1(IIC_CT_SDA_Pin)*2;
            IIC_CT_SDA_GPIO_Port->MODER&=~(3<<(hb));
            if(!in) IIC_CT_SDA_GPIO_Port->MODER|=1<<hb;
            break;
        }
        case SCNS_IIC_SOFT_1:
        {
            const uint32 hb=scnsMathGetHighest1(IIC_SOFT_1_SDA_Pin)*2;
            IIC_SOFT_1_SDA_GPIO_Port->MODER&=~(3<<(hb));
            if(!in) IIC_SOFT_1_SDA_GPIO_Port->MODER|=1<<hb;
            break;
        }
        case SCNS_IIC_SOFT_DCMI:
        {
            const uint32 hb=scnsMathGetHighest1(IIC_DCMI_SDA_Pin)*2;
            IIC_DCMI_SDA_GPIO_Port->MODER&=~(3<<(hb));
            if(!in) IIC_DCMI_SDA_GPIO_Port->MODER|=1<<hb;
            break;
        }
        default:;
    }

}

void scnsBspIicSetScl(ScnsIicEnum iicN,uint8 dat)
{
    switch(iicN)
    {
        case SCNS_IIC_SOFT_CT:
        {
            IIC_CT_SCL_GPIO_Port->BSRR=(dat)?(IIC_CT_SCL_Pin):(IIC_CT_SCL_Pin<<16);
            break;
        }
        case SCNS_IIC_SOFT_1:
        {
            IIC_SOFT_1_SCL_GPIO_Port->BSRR=(dat)?(IIC_SOFT_1_SCL_Pin):(IIC_SOFT_1_SCL_Pin<<16);
            break;
        }
        case SCNS_IIC_SOFT_DCMI:
        {
            IIC_DCMI_SCL_GPIO_Port->BSRR=(dat)?(IIC_DCMI_SCL_Pin):(IIC_DCMI_SCL_Pin<<16);
            break;
        }
        default:;
    }
}

void scnsBspIicSetSda(ScnsIicEnum iicN,uint8 dat)
{
    switch(iicN)
    {
        case SCNS_IIC_SOFT_CT:
        {
            IIC_CT_SDA_GPIO_Port->BSRR=(dat)?(IIC_CT_SDA_Pin):(IIC_CT_SDA_Pin<<16);
            break;
        }
        case SCNS_IIC_SOFT_1:
        {
            IIC_SOFT_1_SDA_GPIO_Port->BSRR=(dat)?(IIC_SOFT_1_SDA_Pin):(IIC_SOFT_1_SDA_Pin<<16);
            break;
        }
        case SCNS_IIC_SOFT_DCMI:
        {
            IIC_DCMI_SDA_GPIO_Port->BSRR=(dat)?(IIC_DCMI_SDA_Pin):(IIC_DCMI_SDA_Pin<<16);
            break;
        }
        default:;
    }
}

uint8 scnsBspIicGetSda(ScnsIicEnum iicN)
{
    switch(iicN)
    {
        case SCNS_IIC_SOFT_CT: return (IIC_CT_SDA_GPIO_Port->IDR&IIC_CT_SDA_Pin)?1:0;
        case SCNS_IIC_SOFT_1: return (IIC_SOFT_1_SDA_GPIO_Port->IDR&IIC_SOFT_1_SDA_Pin)?1:0;
        case SCNS_IIC_SOFT_DCMI: return (IIC_DCMI_SDA_GPIO_Port->IDR&IIC_DCMI_SDA_Pin)?1:0;
        default:;
    }
    return 0;
}

#endif
